package center

import (
	"fmt"
)

func user_change_scene(user_info *User_info, pre_scene *Scene_info, cur_scene *Scene_info) {
	user_info.rwlock_user.RLock()
	defer user_info.rwlock_user.RUnlock()
	p := newPacket()
	p.cmd_code = 0x0041
	p.data = make([]byte, 0)
	t1 := [20]byte{}
	copy(t1[:], user_info.user_name)
	p.data = append(p.data, int32ToByte(user_info.user_id)...)
	p.data = append(p.data, t1[:]...)
	p.data = append(p.data, []byte{user_info.headid}...)
	p.data = append(p.data, int32ToByte(user_info.pos.x)...)
	p.data = append(p.data, int32ToByte(user_info.pos.y)...)
	p.data_len = 33

	cur_scene.rwlock_scene.RLock()
	defer cur_scene.rwlock_scene.RUnlock()
	for _, val := range cur_scene.user_list {
		_user_info := val
		if _user_info == user_info {
			continue
		}
		socket := idtosocket[_user_info.user_id]
		if socket == nil {
			fmt.Println("socket not found!")
			continue
		}
		fmt.Println("socket send to user!", _user_info.user_id)
		p.send(socket)
	}
	//广播给前场景玩家:本玩家已退出
	if pre_scene == nil {
		fmt.Println("no pre scene!")
		return
	}
	if pre_scene == cur_scene {
		fmt.Println("user scene no change:", pre_scene.scene_id)
		return
	}
	p.cmd_code = 0x0043
	p.data = make([]byte, 0)
	p.data = append(p.data, int32ToByte(user_info.user_id)...)
	p.data_len = 4

	pre_scene.rwlock_scene.RLock()
	defer pre_scene.rwlock_scene.RUnlock()
	for _, val := range pre_scene.user_list {
		_user_info := val
		if _user_info == user_info {
			continue
		}
		socket := idtosocket[_user_info.user_id]
		if socket == nil {
			continue
		}
		p.send(socket)
	}
}

//玩家在场景里移动,需要广播给所有同场景玩家
func p0042(p *Packet, coni *Conninfo) {
	user_id := byteToInt32(p.data[0:4])
	pos_x := byteToInt32(p.data[4:8])
	pos_y := byteToInt32(p.data[8:12])
	rwlock_uinfo.RLock()
	user_info := user_info_map[user_id]
	rwlock_uinfo.RUnlock()
	if user_info == nil {
		fmt.Println("user not found!")
		return
	}
	user_info.rwlock_user.RLock()
	defer user_info.rwlock_user.RUnlock()
	scene_id := user_info.scene_id

	scene_List_lock.RLock()
	scene_info := scene_List[scene_id]
	scene_List_lock.RUnlock()

	if scene_info == nil {
		fmt.Println("scene not found!", scene_id)
		return
	}
	scene_info.rwlock_scene.RLock()
	defer scene_info.rwlock_scene.RUnlock()
	user_info.pos.x = pos_x
	user_info.pos.y = pos_y
	for _, val := range scene_info.user_list {
		user_info = val
		socket := idtosocket[user_info.user_id]
		if socket == nil {
			continue
		}
		p.send(socket)
	}
}

//玩家退出场景
func exit_scene(user_id int32) {
	rwlock_uinfo.RLock()
	user_info := user_info_map[user_id]
	rwlock_uinfo.RUnlock()
	if user_info == nil {
		fmt.Println("user not found!")
		return
	}
	user_info.rwlock_user.RLock()
	defer user_info.rwlock_user.RUnlock()
	scene_id := user_info.scene_id
	scene_List_lock.RLock()
	scene_info := scene_List[scene_id]
	scene_List_lock.RUnlock()
	if scene_info == nil {
		fmt.Println("scene not found!", scene_id)
		return
	}
	fmt.Println("user exit scene!!", user_id)
	scene_info.rwlock_scene.Lock()
	defer scene_info.rwlock_scene.Unlock()
	p := newPacket()
	p.cmd_code = 0x0043
	p.data = make([]byte, 0)
	p.data = append(p.data, int32ToByte(user_id)...)
	p.data_len = 4
	for key, val := range scene_info.user_list {
		_user_info := val
		if user_id == _user_info.user_id {
			delete(scene_info.user_list, key)
			fmt.Println("user exit scene!", user_id)
		}
		socket := idtosocket[_user_info.user_id]
		if socket == nil {
			continue
		}
		p.send(socket)
	}
}

//聊天，世界，国家，地区
func p0061(p *Packet, coni *Conninfo) {
	for _, val := range idtosocket { //广播给所有在线玩家，让客户端根据channel值筛选信息
		p.send(val)
	}
}

//一对一私聊
func p0062(p *Packet, coni *Conninfo) {
	user_name := bytesTostring(p.data[0:20])
	var user_id int32
	for _, val := range user_info_map {
		if user_name == val.user_name {
			user_id = val.user_id
			break
		}
	}
	var soc *Conninfo = idtosocket[user_id]
	if soc != nil {
		p.send(soc)
	}
}
